package com.jic.member.impl;

import com.jic.common.base.vo.AdminOperate;
import com.jic.common.base.vo.Page;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.RestResult;
import com.jic.manager.api.manage.SysDeptApi;
import com.jic.manager.request.orgcenter.SysDeptTreeRequest;
import com.jic.manager.response.SysDeptResponse;
import com.jic.manager.service.SysDeptService;
import com.jic.member.common.util.BeanUtil;
import com.jic.member.entity.MememberIntegralBase;
import com.jic.member.enums.EXCEPTION;
import com.jic.member.mapper.MememberIntegralBaseMapper;
import com.jic.member.request.MememberIntegralBaseQueryRequest;
import com.jic.member.request.MememberIntegralBaseRequest;
import com.jic.member.service.MememberIntegralBaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 会员积分基础表
 *
 * @author :
 * @version : v1.0.0
 * @email :
 * @since : 2020-04-04 02:29:49
 */
@Service
public class MememberIntegralBaseServiceImpl implements MememberIntegralBaseService {

    private SysDeptService sysDeptService;

    @Autowired
    private SysDeptApi sysDeptApi;

    private final MememberIntegralBaseMapper mememberIntegralBaseMapper;

    public MememberIntegralBaseServiceImpl(MememberIntegralBaseMapper mememberIntegralBaseMapper) {
        this.mememberIntegralBaseMapper = mememberIntegralBaseMapper;
    }

    @Override
    public long insert(MememberIntegralBase mememberIntegralBase) {
        long flag = mememberIntegralBaseMapper.insert(mememberIntegralBase);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return mememberIntegralBaseMapper.delete(id);
    }

    @Override
    public long update(MememberIntegralBase mememberIntegralBase) {
        return mememberIntegralBaseMapper.update(mememberIntegralBase);
    }

    @Override
    public MememberIntegralBase queryByPrimaryKey(Long id) {
        return mememberIntegralBaseMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<MememberIntegralBase> query(MememberIntegralBase mememberIntegralBase) {
        return mememberIntegralBaseMapper.query(mememberIntegralBase);
    }

    @Override
    public PageResult<MememberIntegralBase> queryPage(MememberIntegralBase mememberIntegralBase, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<MememberIntegralBase> list = mememberIntegralBaseMapper.queryPage(startIndex, pageQuery.getLimit(), mememberIntegralBase);
        long count = mememberIntegralBaseMapper.queryPageCount(mememberIntegralBase);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResult insertIntegralBaseInfo(MememberIntegralBaseRequest mememberIntegralBaseRequest) {
        RestResult restResult = new RestResult();
        AdminOperate admin = mememberIntegralBaseRequest.getAdminOperate();
        if (admin.getDeptType().getCode() == 0) {
            MememberIntegralBase mememberIntegralBase = new MememberIntegralBase();
            //新增会员积分基础信息
            final Integer maxGrowthValue = mememberIntegralBaseRequest.getMaxGrowthValue();
            mememberIntegralBase.setDeleteFlag(0);
            mememberIntegralBase.setStatus(1);
            mememberIntegralBase.setDeptId(admin.getDeptId());
            mememberIntegralBase.setCreatorName(admin.getOperateName());
            mememberIntegralBase.setUpdaterName(admin.getOperateName());
            mememberIntegralBase.setRmb(mememberIntegralBaseRequest.getRmb());
            mememberIntegralBase.setIntegral(mememberIntegralBaseRequest.getIntegral());
            mememberIntegralBase.setMaxGrowthValue(mememberIntegralBaseRequest.getMaxGrowthValue());
            mememberIntegralBase.setDailyCeiling(mememberIntegralBaseRequest.getDailyCeiling());
            mememberIntegralBase.setEveryCeiling(mememberIntegralBaseRequest.getEveryCeiling());
            mememberIntegralBase.setCreator(admin.getOperator());
            mememberIntegralBase.setUpdater(admin.getOperator());
            int insert = mememberIntegralBaseMapper.insert(mememberIntegralBase);
            if (insert > 0) {
                restResult.setCode(EXCEPTION.SUCCESS.getName());
                restResult.setData(mememberIntegralBase);
                restResult.setMsg(EXCEPTION.SUCCESS.getDesc());
                return restResult;
            } else {
                restResult.setCode(EXCEPTION.FAIL.getName());
                restResult.setMsg(EXCEPTION.FAIL.getDesc());
            }
        } else {
            restResult.setCode(EXCEPTION.FAIL.getName());
            restResult.setMsg("属于母平台权限，其他组织成员无权限!");
        }
        return restResult;
    }

    @Override
    public RestResult queryByDeptId(MememberIntegralBaseQueryRequest mememberIntegralBaseQueryRequest) {
        RestResult restResult = new RestResult();

        MememberIntegralBase mememberIntegralBase = BeanUtil.copy(mememberIntegralBaseQueryRequest, MememberIntegralBase.class);
        mememberIntegralBase.setDeleteFlag(0);
        mememberIntegralBase.setStatus(1);
        final MememberIntegralBase query = mememberIntegralBaseMapper.queryByDeptId(mememberIntegralBase);
        if (null != query) {
            restResult.setCode(EXCEPTION.SUCCESS.getName());
            restResult.setData(query);
            return restResult;
        } else {
            restResult.setCode(EXCEPTION.ISNULL.getName());
            restResult.setMsg(EXCEPTION.ISNULL.getDesc());
        }
        return restResult;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResult insertStoreIntegralBaseInfo(MememberIntegralBaseRequest mememberIntegralBaseRequest) {
        RestResult restResult = new RestResult();
        AdminOperate admin = mememberIntegralBaseRequest.getAdminOperate();
        if (admin.getDeptType().getCode() != 0) {
            MememberIntegralBase mememberIntegralBase = new MememberIntegralBase();
            //新增会员积分基础信息
            final Integer maxGrowthValue = mememberIntegralBaseRequest.getMaxGrowthValue();
            mememberIntegralBase.setDeleteFlag(0);
            mememberIntegralBase.setStatus(1);
            mememberIntegralBase.setDeptId(admin.getDeptId());
            mememberIntegralBase.setCreatorName(admin.getOperateName());
            mememberIntegralBase.setUpdaterName(admin.getOperateName());
            mememberIntegralBase.setRmb(mememberIntegralBaseRequest.getRmb());
            mememberIntegralBase.setIntegral(mememberIntegralBaseRequest.getIntegral());
            mememberIntegralBase.setMaxGrowthValue(mememberIntegralBaseRequest.getMaxGrowthValue());
            mememberIntegralBase.setDailyCeiling(mememberIntegralBaseRequest.getDailyCeiling());
            mememberIntegralBase.setEveryCeiling(mememberIntegralBaseRequest.getEveryCeiling());
            mememberIntegralBase.setCreator(mememberIntegralBaseRequest.getAdminOperate().getOperator());
            mememberIntegralBase.setUpdater(mememberIntegralBaseRequest.getAdminOperate().getOperator());
            if (null != maxGrowthValue && maxGrowthValue != 0) {
                Date date = new Date();
                Calendar cal = Calendar.getInstance();
                cal.setTime(date);
                cal.add(Calendar.YEAR, maxGrowthValue);
                mememberIntegralBase.setMaxTime(cal.getTime());
            }
            int insert = mememberIntegralBaseMapper.insert(mememberIntegralBase);
            if (insert > 0) {
                restResult.setCode(EXCEPTION.SUCCESS.getName());
                restResult.setData(mememberIntegralBase);
                restResult.setMsg(EXCEPTION.SUCCESS.getDesc());
                return restResult;
            } else {
                restResult.setCode(EXCEPTION.FAIL.getName());
                restResult.setMsg(EXCEPTION.FAIL.getDesc());
            }
        } else {
            restResult.setCode(EXCEPTION.FAIL.getName());
            restResult.setMsg("属于子平台权限，其他组织成员无权限!");
        }
        return restResult;
    }



    @Override
    public PageResult<MememberIntegralBase> queryPageForChild(MememberIntegralBaseQueryRequest mememberIntegralBase, Page page) {
        RestResult<List<SysDeptResponse>> res = new RestResult<List<SysDeptResponse>>();
        if (mememberIntegralBase.getDeptId() == null) {
            SysDeptTreeRequest sysDeptTreeRequest = new SysDeptTreeRequest();
            sysDeptTreeRequest.setAdminOperate(mememberIntegralBase.getAdminOperate());
            res = sysDeptApi.getDeptList(sysDeptTreeRequest);
            for (SysDeptResponse datum : res.getData()) {
                mememberIntegralBase.getDeptIds().add(datum.getId());
            }

            List<Long> deptIds = mememberIntegralBase.getDeptIds();
            mememberIntegralBase.setIds(new ArrayList<Long>());
            for (Long deptId : deptIds) {
                Long id = mememberIntegralBaseMapper.queryIdByDeptId(deptId);
                if (id != null) {
                    mememberIntegralBase.getIds().add(id);
                }
            }
            //计算下标
            int startIndex = (page.getStart() - 1) * page.getLimit();

            List<MememberIntegralBase> list = mememberIntegralBaseMapper.queryPageForChild(startIndex, page.getLimit(), mememberIntegralBase);
            for (MememberIntegralBase integralBase : list) {
                Long deptId = integralBase.getDeptId();
                for (SysDeptResponse datum : res.getData()) {
                    if (deptId.equals(datum.getId())) {
                        integralBase.setDeptName(datum.getDeptName());
                    }
                }
            }
            long count = mememberIntegralBaseMapper.queryPageCountForChild(mememberIntegralBase);
            PageResult pageResult = new PageResult();
            pageResult.setRows(list);
            pageResult.setTotal(count);
            return pageResult;

        } else {
            mememberIntegralBase.setIds(new ArrayList<Long>());
            Long id = mememberIntegralBaseMapper.queryIdByDeptId(mememberIntegralBase.getDeptId());
            mememberIntegralBase.getIds().add(id);
            //计算下标
            int startIndex = (page.getStart() - 1) * page.getLimit();
            List<MememberIntegralBase> list = mememberIntegralBaseMapper.queryPageForChild(startIndex, page.getLimit(), mememberIntegralBase);
            SysDeptTreeRequest sysDeptTreeRequest = new SysDeptTreeRequest();
            sysDeptTreeRequest.setAdminOperate(mememberIntegralBase.getAdminOperate());
            res = sysDeptApi.getDeptList(sysDeptTreeRequest);
            for (MememberIntegralBase integralBase : list) {
                Long deptId = integralBase.getDeptId();

                for (SysDeptResponse datum : res.getData()) {
                    if (deptId.equals(datum.getId())) {
                        integralBase.setDeptName(datum.getDeptName());
                    }
                }
            }
            long count = mememberIntegralBaseMapper.queryPageCountForChild(mememberIntegralBase);
            PageResult pageResult = new PageResult();
            pageResult.setRows(list);
            pageResult.setTotal(count);
            return pageResult;

        }
    }
}